﻿//------------------------------------------------------------------------------------------------- 
// <copyright file="SetWebPartProperties.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>Sets the WebPart properties.</summary>
//-------------------------------------------------------------------------------------------------
namespace Collaboris.Saf.Actions.Wss.WebPart
{
    using System;

    using Engine;

    using Entities;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebPartPages;

    using SharePoint.Wss.Attributes;
    using SharePoint.Wss.Files;
    using SharePoint.Wss.WebParts;

    using Utils;

    [CollaborisClass(
        Title = "Sets the WebPart properties",
        ReleaseStatus = ReleaseStatus.Development,
        DisableDocumentation = false,
        ContactAuthorEmail = "Support@Collaboris.co.uk",
        Summary = "Sets the WebPart properties in a page.",
        ClassGuid = "{156E17A5-A7BA-4e1f-8A48-1BC341A7C665}"
        )
    ]
    public class SetWebPartProperties : WSSActionBase<WebPartPropertiesInfo>
    {
        /// <summary>
        /// Does the action.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        protected override ProcessStatus DoAction(SafContext context, WebPartPropertiesInfo entity)
        {
            this.Trace.TraceMethodStart("DoAction");
            
            // Set the WebPart properties now...
            SPWebPartHelper.SetWebPartProperties(TargetWeb,
                                                 entity.WebPartPageRelativeUrl, 
                                                 entity.WebPartID, 
                                                 entity.PersonalizationScope, 
                                                 entity.Properties);

            this.Trace.TraceMethodEnd("DoAction");
            
            return ProcessStatus.Success;
        }

        /// <summary>
        /// Checks the Data entity used by the Action for Validity
        /// </summary>
        /// <param name="context">The <see cref="SafContext"/> passed in by the SAF Engine.</param>
        /// <param name="entity">The data entity to Validate.</param>
        /// <param name="errors">List of <see cref="ValidationError"/>s</param>
        /// <param name="mode"><see cref="ProcessMode"/>signalling wheter we are Doing, or Undoing the action.</param>
        /// <returns>false if invalid; otherwise true.</returns>
        protected override bool IsValid(SafContext context, WebPartPropertiesInfo entity,
            ref System.Collections.Generic.List<ValidationError> errors, ProcessMode mode)
        {
            int currentCount = errors.Count;

            // Validate each object now.
            ValidateStringForNull(entity.WebPartPageRelativeUrl, "WebPartListInfo.WebPartPageRelativeUrl", ref errors);
            ValidateObjectForNull(entity.Properties, "WebPartListInfo.Properties", ref errors);
            ValidateStringForNull(entity.WebPartID, "WebPartListInfo.WebPartID", ref errors);

            // Have we added errors ?
            return errors.Count == currentCount;
        }
    }
}